package route

import (
	"common-admin/controller"
	"common-admin/e"
	"common-admin/util"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
	"time"
)

//基本请求响应demo
func loadRouteDemo(r *gin.Engine) {
	//string
	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "pong")
	})
	//json
	r.GET("/user", func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"nam":   "ztind",
			"sex":   "man",
			"hoppy": []interface{}{"music", "sports", "movie"},
			"info": map[string]interface{}{
				"age":    18,
				"weight": "90kg",
				"height": "172cm",
			},
		})
	})
	//jsonp
	r.GET("/JSONP?callback=x", func(c *gin.Context) {
		data := map[string]interface{}{
			"foo": "bar",
		}
		//callback is x
		// Will output  :   x({\"foo\":\"bar\"})
		c.JSONP(http.StatusOK, data)
	})
	//AsciiJSON
	r.GET("/someAsciiJSON", func(c *gin.Context) {
		data := map[string]interface{}{
			"lang": "GO语言",
			"tag":  "<br>",
		}
		// will output : {"lang":"GO\u8bed\u8a00","tag":"\u003cbr\u003e"}
		c.AsciiJSON(http.StatusOK, data)
	})
	//PureJSON  纯json字符串输出
	r.GET("/purejson", func(c *gin.Context) {
		c.PureJSON(200, gin.H{
			"html": "<b>Hello, world!</b>",
		})
	})
}

//redis请求测试
func loadRouteTest4Redis(r *gin.Engine) {
	r.GET("/redis/set", func(c *gin.Context) {
		rds, err := util.GetRedigo()
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"code": 1, "message": err.Error()})
		}
		key := c.DefaultQuery("key", "test")
		value := c.DefaultQuery("value", "123")
		expire, _ := strconv.Atoi(c.DefaultQuery("expire", "0"))
		fmt.Println(key)
		fmt.Println(value)
		if expire > 0 {
			err = rds.SetEx(key, value, expire)
		} else {
			err = rds.Set(key, value)
		}
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": "ok"})
	})

	r.GET("/redis/get", func(c *gin.Context) {
		rds, err := util.GetRedigo()
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"code": 1, "message": err.Error()})
		}
		key := c.DefaultQuery("key", "test")
		fmt.Println(key)
		value, err := rds.Get(key)
		c.JSON(http.StatusOK, gin.H{"code": 0, "data": value, "message": "ok"})
	})

	r.GET("/redis/del", func(c *gin.Context) {
		rds, err := util.GetRedigo()
		if err != nil {
			c.JSON(http.StatusInternalServerError, gin.H{"code": 1, "message": err.Error()})
		}
		key := c.DefaultQuery("key", "test")
		fmt.Println(key)
		err = rds.Del(key)
		message := ""
		if err != nil {
			message = err.Error()
		}
		c.JSON(http.StatusOK, gin.H{"code": 0, "message": message})
	})
}

func LoadRoute(r *gin.Engine) {
	loadRouteDemo(r)
	loadRouteTest4Redis(r)
	r.GET("/orders", controller.Show)
	r.GET("/test", func(c *gin.Context) {
		token, _ := util.CreatToken(1)
		c.JSON(http.StatusOK, util.ReturnData(e.SUCCESS, e.GetMsg(e.SUCCESS), token))
	})
	r.POST("/test2", func(c *gin.Context) {
		claim, err := util.ParseToken(c.PostForm("token"))
		code := e.SUCCESS
		var data interface{}
		if err != nil {
			code = e.ERROR_VERIFY_CAPTCHA
		}
		nowTime := time.Now().Unix()
		if claim == nil || claim.ExpiresAt < nowTime {
			code = e.ERROR_TOKEN_TIMEOUT
		} else {
			data = claim
		}
		c.JSON(http.StatusOK, util.ReturnData(code, e.GetMsg(code), data))
	})
	r.GET("/captcha", func(c *gin.Context) {
		id, b64s, err := util.CreatCaptcha("math")
		code := e.SUCCESS
		var data interface{}
		if err != nil {
			code = e.ERROR_CREATE_CAPTCHA
			data = ""
		} else {
			data = map[string]string{"id": id, "value": b64s}
		}
		c.JSON(http.StatusOK, util.ReturnData(code, e.GetMsg(code), data))
	})
	r.POST("/login", controller.Login)
	r.POST("/menu/test", controller.Test)
}
